package com.gamesnet.ihm;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.gamesnet.api.Api;
import com.gamesnet.api.Session;
import com.gamesnet.models.Utilisateur;

public class InfoCompteActivity extends Activity {
	
	Utilisateur user = null;
	long userId;

	@Override
	public void onCreate(Bundle savedInstanceState) { 

		Bundle b = getIntent().getExtras();
		userId = b.getLong("userId");
		
		if(Session.getInstance().getUser().getId() == userId)
			user = Session.getInstance().getUser();
		else{
			try {
				user = Api.getUser(String.valueOf(userId));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		super.onCreate(savedInstanceState); 
		setContentView(R.layout.infocompte);

		ImageView avatar = (ImageView)findViewById(R.id.avatar);

		Bitmap bitmap = null;

		try {
			URL urlImage = new URL("http://10.0.2.2:8080/Gamesnet/images/upload/resized/avatar/"+user.image()+".jpg");
			HttpURLConnection connection = (HttpURLConnection) urlImage.openConnection();
			InputStream inputStream = connection.getInputStream();
			bitmap = BitmapFactory.decodeStream(inputStream);
			avatar.setImageBitmap(bitmap);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		TextView email = (TextView)findViewById(R.id.userEmail);
		email.setText(user.getMail());

		TextView firstname = (TextView)findViewById(R.id.userFirstname);
		firstname.setText(user.getPrenom());

		TextView name = (TextView)findViewById(R.id.userName);
		name.setText(user.getNom());

		TextView address = (TextView)findViewById(R.id.userAdress);
		address.setText(user.getAdresse());

		TextView zipCode = (TextView)findViewById(R.id.userZipCode);
		zipCode.setText(user.getCodePostal());

		TextView city = (TextView)findViewById(R.id.userCity);
		city.setText(user.getVille());


	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, 1, 0, "Voir son mur");
		return true; 
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) { 
		case 1:
			Log.v("compte", "clique le menu item");
			Intent intent = new Intent(this, WallActivity.class);
			intent.putExtra("userId", user.getId());
			startActivity(intent);
			return true;
		default:
			return true;
		} 
	}

}
